#!/usr/bin/env python


def PLex():
	plainFile = open("test.txt","r+")
	stream = plainFile.read()
	print "Lexim"+"\t\t"+"Type"
	getIdTokens(stream)
	getDigitsTokens(stream)
	getOperatorsTokens(stream)
	getInvalidTokens(stream)
	plainFile.close()


def scanWhiteSpace(scannedText):
	if (scannedText == " ") or (scannedText == "") or (scannedText == "\n") or (scannedText == "\t"):
		return True
	else:
		return False



def scanLetter(scannedText):
	if (scannedText >= 'A' and scannedText <= 'Z') or (scannedText >= 'a' and scannedText <= 'z') or (scannedText =='_'):
		return True
	else:
		return False



def scanDigits(scannedText):
	if (scannedText >= '0' and scannedText <= '9'):
		return True
	else:
		return False



def scanOperators(scannedText):
	if (scannedText == '+') or (scannedText == '-') or (scannedText =='*') or (scannedText == '='):
		return True
	else:
		return False





def getIdTokens(scannedText):
	"""
	TODO: Fix one problem
		* if the token contains any not char string in the begining of token stransform it to invalid token
	"""
	buffStr=""
	state=0
	for i in scannedText:
		if scanLetter(i) == True:
			buffStr+=i
			state=1
		elif (scanDigits(i) == True) and (state == 1) and (scanLetter(buffStr) == True):
			buffStr+=i
		
		else:
			if (scanWhiteSpace(buffStr) == True):
				continue
			print buffStr+"\t\t"+"Variable"
			buffStr=""	
			




def getDigitsTokens(scannedText):
	"""
	TODO: Fix one problem
		* if the token contains any non digit string stransform it to invalid token
	"""
	buffStr=""
	for i in scannedText:
		if scanDigits(i) == True:
			buffStr+=i
		else:
			if (scanWhiteSpace(buffStr) == True):
				continue
			print buffStr+"\t\t"+"Digit"
			buffStr=""



def getOperatorsTokens(scannedText):
	"""
	Done!
	"""
	buffStr=""
	for i in scannedText:
		if scanOperators(i) == True:
			buffStr=i
			if (scanWhiteSpace(buffStr) == True):
				continue
			print buffStr+"\t\t"+"Math Operator"
		else:
			buffStr=""





def getInvalidTokens(scannedText):
	"""
	Done!
	"""
	buffStr=""
	for i in scannedText:
		if (scanLetter(i) == False) and (scanDigits(i) == False) and (scanOperators(i) == False):
			buffStr=i
			if (scanWhiteSpace(buffStr) == True):
				continue
			print buffStr+"\t\t"+"Invalid Entery"
		
		else:
			buffStr=""
		



	


if __name__ == '__main__':
	PLex()
